<?php
/*
 * Created on 2009-7-19
 *
 * Author: guobin@yizin.net
 */

class com_yizin_platform_RouterHandler extends Extension
{
    public function apply( & $context, Chain $chain)
    {
        $chain->next($context);
        if ($context['rule'] && is_array($context['rule']))
        {
            foreach ($context['rule'] as $pattern=>$page)
            {
                $matches = array ();
                $uri = $context['uri'];
                if (preg_match($pattern, $uri, $matches))
                {
                    global $featureManager;
                    $GLOBALS['page'] = $page;
                    $interceptorContext['uri'] = $uri;
                    $interceptorContext['end'] = create_function('', 'global $page; include ($page);');
                    $featureManager->apply("com.yizin.platform.interceptor", $interceptorContext);
                    unset ($GLOBALS['page']);
                    break;
                }
            }
        }
    }
}


class com_yizin_platform_InterceptorHandler extends Extension
{
    public function apply( & $context, Chain $chain)
    {
        $chain->next($context);
        $uri = $context['uri'];
        $interceptors = array ();
        if ($context['rule'] && is_array($context['rule']))
        {
            foreach ($context['rule'] as $pattern=>$iinterceptors)
            {
                $matches = array ();
                if (preg_match($pattern, $uri, $matches))
                {
                    if ($iinterceptors && is_array($iinterceptors))
                    {
                        foreach ($iinterceptors as $interceptor)
                        {
                            $interceptor->matches = $matches;
                            $interceptors[] = $interceptor;
                        }
                    }
                }
            } 
        }
        $end = $context['end'];
        $interceptorChain = new InterceptorChain($interceptors, $end);
        $interceptorChain->next($context);
    }
}

class com_yizin_platform_PlatformFeature extends Feature
{
    public function registerExtension()
    {
        $this->extensions = Array(
        new com_yizin_platform_RouterHandler('com.yizin.platform.router', "", 0),
        new com_yizin_platform_InterceptorHandler('com.yizin.platform.interceptor', "", 0)
        );
    }

    public function registerExtendable()
    {
    }

    public function init()
    {
    }

    public function destroy()
    {
    }
}

$featureClass = "com_yizin_platform_PlatformFeature";
$featureName = "PlatformFeature";
$featureIntroduction = "Platform feature";
?>
